<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container>
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>

          <a-col :span="6">
            <a-form-model-item label="订单编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="model.schedulingNumber"  ></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="6">
            <a-form-model-item label="评审表编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="model.code" disabled ></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="6">
            <a-form-model-item label="订单类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list"  dictCode="document_type" v-model="model.documentType" disabled   />
            </a-form-model-item>
          </a-col>

          <a-col :span="6">
            <a-form-model-item label="审批流程" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="list"
                :disabled="formDisabled"
                v-model="model.flowFlag"
                dictCode="trade_order_flow"
                placeholder="请选择审批流程"
              />
            </a-form-model-item>
          </a-col>

          <a-col :span="6">
            <a-form-model-item label="客户编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="model.customerCode"  disabled ></a-input>

            </a-form-model-item>
          </a-col>

          <a-col :span="6">
            <a-form-model-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <a-input v-model="model.customerName" disabled ></a-input>

            </a-form-model-item>
          </a-col>

          <a-col :span="6">

            <a-form-model-item  label="客户类型" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <j-dict-select-tag type="list"  dictCode="customer_category" v-model="model.customer"  disabled />
            </a-form-model-item>

          </a-col>

          <a-col :span="6">

            <a-form-model-item  label="客户国家" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <j-dict-select-tag type="list"  dictCode="erp_country,country_name,country_id" v-model="model.customerCountryId " disabled  />
            </a-form-model-item>

          </a-col>

          <a-col :span="6">
            <a-form-model-item  label="订单年份" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <a-input v-model="model.year"  disabled ></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="6">

            <a-form-model-item  label="业务员" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <j-select-user-by-dep-v2 v-model="model.salesman" disabled :multi="true" />
            </a-form-model-item>

          </a-col>


          <a-col :span="6">

            <a-form-model-item  label="包材到位" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <a-input v-model="model.arrived"  ></a-input>
            </a-form-model-item>

          </a-col>

          <a-col :span="6">
            <a-form-model-item label="交货日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="model.deliveryTime"  :disabled="formDisabled"></a-input>
            </a-form-model-item>
          </a-col>
        <a-col :span="24">
          <a-form-model-item label="备注" :labelCol="{xs: { span: 24 },sm: { span: 2 }}" :wrapperCol="{xs: { span: 24 },sm: { span: 22 }}">
            <a-textarea
              v-model="model.notes"
              :disabled="formDisabled"
              :rows="2"
              allow-clear
              showCount
            ></a-textarea>
          </a-form-model-item>
        </a-col>

          <a-col :span="24">
            <a-card class="mycard">
              <a-row :gutter="10">
                <a-col :span="4">
                  <h4>产品信息</h4>
                </a-col>
              </a-row>
<!--              y: 'calc(100vh-350px)'-->
              <a-table
                class="mytable"
                bordered
                :ref="refKeys[0]"
                :scroll="{ x: 650 }"
                :data-source="model.erpGoodsPackageList"
                :columns="goodsColumns"
                :rowKey="(record) => record.rowKey"
                :selected="onSelectChange"
                :row-selection="{ type: 'radio', selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
                :pagination="false"
              >
                <template slot="number" slot-scope="text, record">
                  <edit-number-cell-2
                    :text="text"
                    :disabled="formDisabled"
                    @change="onCellChange(record, 'number', $event)"
                  ></edit-number-cell-2>
                </template>

                <template slot="isProduction" slot-scope="text, record">
                  <j-dict-select-tag type="list"  dictCode="yn" @change="onCellChange(record, 'isProduction', $event)" v-model="text" :disabled="formDisabled"  />
                </template>

                <template slot="mainQuantity" slot-scope="text, record">
                  <edit-number-cell-2
                    :text="text"
                    :disabled="formDisabled"
                    @change="onMainQuantityCellChange(record, 'mainQuantity', $event)"
                  ></edit-number-cell-2>
                </template>

                <template slot="meUnit" slot-scope="text">
                  <span>{{ unitDictOptions[text] }}</span>
                </template>
                <template slot="auUnit" slot-scope="text">
                  <span>{{ zlunitDictOptions[text] }}</span>
                </template>

              </a-table>
            </a-card>
          </a-col>

          <a-col :span="24">
            <a-card class="mycard">
              <a-row :gutter="10">
                <a-col :span="4">
                  <h4>包材信息</h4>
                </a-col>
              </a-row>
              <j-vxe-table
                keep-source
                :ref="refKeys[1]"
                bordered
                :dataSource="model.packageList"
                :columns="packageColumns"
                :disabled="formDisabled"
                class="bold-border"
              >
                <template v-slot:isBrought="{ row }">
                  <j-dict-select-tag :disabled="formDisabled" v-model="row.isBrought" dictCode="yn"/>
                </template>

                <template v-slot:packageInOutType="{ row }">
                  <j-dict-select-tag :disabled="formDisabled" v-model="row.packageInOutType" dictCode="package_type"/>
                </template>
                <template v-slot:proportion="{ row }">

                  <a-input-number :disabled="formDisabled" style="width:100%" v-model="row.proportion" :default-value="0" />
                </template>

                <template v-slot:action="{ row }">
                  <a v-if="row.patternFile" @click="handlePreviewFile(row)">查看版样</a>
                </template>
              </j-vxe-table>


            </a-card>
          </a-col>

          <a-col :span="12">
            <a-form-model-item  label="一、配方要求" :labelCol="labelCol2" :wrapperCol="wrapperCol2" >
              <a-textarea v-model="model.formulaReq" :rows="3" allow-clear showCount :disabled="formDisabled"></a-textarea>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item  label="三、打码要求" :labelCol="labelCol2" :wrapperCol="wrapperCol2" >
              <a-textarea v-model="model.productionRemark" :rows="3" allow-clear showCount :disabled="formDisabled"></a-textarea>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item  label="二、包装要求" :labelCol="labelCol2" :wrapperCol="wrapperCol2" >
              <a-textarea v-model="model.packReq" :rows="3" allow-clear showCount :disabled="formDisabled"></a-textarea>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item  label="四、评审备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2" >
              <a-textarea v-model="model.quoteRemark" :rows="3" allow-clear showCount :disabled="formDisabled"></a-textarea>
            </a-form-model-item>
          </a-col>

          <a-col :span="6">
            <a-form-model-item label="订货数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="weight">
              <a-row>
                <a-col :span="20">
                  <a-input-number v-model="model.weight" :disabled="formDisabled" style="width: 100%"  />
                </a-col>
                <a-col :span="4">
                  <j-dict-select-tag
                    type="list"
                    :disabled="formDisabled"
                    v-model="model.weightUnit"
                    dictCode="zl_unit"
                  />
                </a-col>
              </a-row>
            </a-form-model-item>
          </a-col>

          <a-col :span="6">
            <a-form-model-item label="发货柜数" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <a-input v-model="model.cabinetCount"   disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="发货港" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <a-input v-model="model.shipPort"   disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="目的港" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="model.destPort"  disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="成交方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="model.transactionMethod"   disabled></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="6">
            <a-form-model-item label="客户其他费用" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-model="model.otherExpenses"  style="width: 100%"  :disabled="formDisabled"></a-input-number>
            </a-form-model-item>
          </a-col>

          <a-col :span="6">
            <a-form-model-item label="客户订单费用" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-model="model.orderCost"  style="width: 100%" :disabled="formDisabled"></a-input-number>
            </a-form-model-item>
          </a-col>

          <a-col :span="6">
            <a-form-model-item label="货款总值" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-row>
                <a-col :span="20">

                  <a-input-number v-model="model.ywActualQuote"  style="width: 100%"  disabled/>
                </a-col>
                <a-col :span="4">
                  <j-dict-select-tag
                    type="list"
                    disabled
                    v-model="model.moneyUnit"
                    dictCode="money_unit"
                  />
                </a-col>
              </a-row>
            </a-form-model-item>
          </a-col>

          <a-col :span="6">
            <a-form-model-item label="付款方式" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <a-input v-model="model.payMethod" disabled ></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="6">
            <a-form-model-item label="到货后付全款天数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-model="model.daysArrival"  style="width: 100%"  disabled></a-input-number>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="财务备录" :labelCol="{xs: { span: 24 },sm: { span: 4 }}" :wrapperCol="{xs: { span: 24 },sm: { span: 20 }}">
              <a-input v-model="model.financeRemark" :disabled="formDisabled"  ></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item v-show ="model.status==999" label="特殊说明" :labelCol="{xs: { span: 24 },sm: { span: 2 }}" :wrapperCol="{xs: { span: 24 },sm: { span: 22 }}">
              <a-textarea
                v-model="model.specialInstructions"
                :disabled="disableSpecial"
                :rows="3"
                allow-clear
                showCount
              ></a-textarea>
            </a-form-model-item>
          </a-col>

        </a-row>

      </a-form-model>

    </j-form-container>
  </a-spin>
</template>

<script>
import {JVxeTableModelMixin} from "@/mixins/JVxeTableModelMixin";
import {getAction, httpAction} from "@api/manage";
import EditNumberCell2 from "../../../../components/table/EditNumberCell2.vue"
import moment from 'moment';
import {JVXETypes} from "@comp/jeecg/JVxeTable";
import { FilePreview } from '@/utils/tslCommon'
import {initDictOptions} from "@comp/dict/JDictSelectUtil";
export default {
  name: "BussTraOrdersForm",
  mixins: [JVxeTableModelMixin],
  components: {
    moment,
    EditNumberCell2
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
    //表单禁用
    disableSpecial: {
      type: Boolean,
      default: false,
      required: false,
    },
    /*流程自定义表单数据*/
    customFormData: {
      type: Object,
      default: () => {
        return {}
      },
      required: false,
    },
  },
  data() {
    return {
      url:{
        productByReviewId: '/review/bussContractRequirementReview/reviewProduct/ByReviewId',
        customerById:'/base/erpCustomer/queryById',
        reviewById:'/review/bussContractReviewDesRequire/queryById',
        add:'/review/crmTradeOrder/add',
        edit:'/review/crmTradeOrder/edit',
        queryCrmTradeOrderGoodsByMainId:'/review/crmTradeOrder/queryCrmTradeOrderGoodsByMainId'
      },
      confirmLoading: false,
      validatorRules: {
        // code: [{ required: true, message: '请输入评审表编号!' }],
        // currency: [{ required: true, message: '请选择币种!' }],
        // flowFlag: [{ required: true, message: '请选择审批流程!' }],
        // schedulingNumber: [
        //   { required: true, message: '请填写销售订单号！（如果是补单，请填写销售订单号；如果是新单，请填写无）!' },
        // ],
      },
      selectedProduct: {},
      selectedRowKeys: [],
      selectedRows:[],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 4 },
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 },
      },
      refKeys:['product','package'],
      goodsColumns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          fixed: 'left',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '客户物料编号',
          dataIndex: 'customCode',
          fixed: 'left',
          width: 150,
        },
        {
          title: '物料编码',
          dataIndex: 'packageCode',
          fixed: 'left',
          width: 150,
        },
        {
          title: '物料名称',
          dataIndex: 'packageName',
          fixed: 'left',
          width: 300,
        },
        {
          title: '规格型号',
          dataIndex: 'noneSpec',
          width: 120,
        },
        {
          title: '配方序号',
          dataIndex: 'number',
          scopedSlots: { customRender: 'number' },
          width: 100,
        },
        {
          title: '是否生产',
          dataIndex: 'isProduction',
          scopedSlots: { customRender: 'isProduction' },
          width: 100,
        },
        {
          title: '包装方式',
          dataIndex: 'packageType',
          width: 120,
        },
        {
          title: '主数量',
          dataIndex: 'mainQuantity',
          scopedSlots: { customRender: 'mainQuantity' },
          width: 120,
        },
        {
          title: '主单位',
          dataIndex: 'meUnit',
          width: 100,
          scopedSlots: { customRender: 'meUnit' },
        },
        {
          title: '辅数量',
          dataIndex: 'auxiliaryQuantity',
          width: 120,
        },
        {
          title: '辅单位',
          dataIndex: 'auUnit',
          width: 100,
          scopedSlots: { customRender: 'auUnit' },
        },
        {
          title: '配方年',
          dataIndex: 'formulaYear',
          width: 100,
        },
        {
          title: '配方编码',
          dataIndex: 'formulaCode',
          width: 150,
        },
        {
          title: '配方号',
          dataIndex: 'formulaNumber',
          width: 150,
        },
        {
          title: '配方名称',
          dataIndex: 'formulaDefine',
          width: 200,
        },
        {
          title: '库存',
          dataIndex: 'quantity',
          width: 150,
        },
      ],
      packageColumns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '物料编号',
          dataIndex: 'packageCode',
          key: 'packageCode',
        },
        {
          title: '物料名称',
          dataIndex: 'packageName',
          key: 'packageName',
        },
        {
          title: '规格型号',
          dataIndex: 'noneSpec',
          key: 'noneSpec',
        },
        {
          title: '是否自带',
          dataIndex: 'isBrought',
          key: 'isBrought',
          type: JVXETypes.slot,
          slotName:'isBrought',
          /*          type: JVXETypes.select,
                    options: [
                      // 下拉选项
                      { title: '是', value: '1' },
                      { title: '否', value: '0' },
                    ],*/
        },
        {
          title: '包材类别',
          dataIndex: 'packageInOutType',
          key: 'packageInOutType',
          type: JVXETypes.slot,
          slotName:'packageInOutType',
          // type: JVXETypes.select,
          // dictCode: 'package_type',
        },
        {
          title: '比例数量',
          dataIndex: 'proportion',
          // type: JVXETypes.input,
          type: JVXETypes.slot,
          slotName:'proportion',
          key: 'proportion',
        },
        {
          title: '损耗率(%)',
          dataIndex: 'lossRate',
          key: 'lossRate',
          type: JVXETypes.input,
        },
        {
          title: '材质',
          dataIndex: 'materialQuality',
          key: 'materialQuality',
        },
        {
          title: '尺寸',
          dataIndex: 'size',
          key: 'size',
        },
        {
          title: '包装要求',
          dataIndex: 'requirements',
          key: 'requirements',
          type: JVXETypes.input,
        },
        {
          title: '版样',
          dataIndex: 'patternFile',
          key: 'patternFile',
          type: JVXETypes.file,
          token: true,
        },
        {
          title: '操作',
          key: 'action',
          type: JVXETypes.slot,
          fixed: 'right',
          minWidth: '100px',
          align: 'center',
          slotName: 'action',
        },
      ],
      unitDictOptions: {},
      zlunitDictOptions: {},
    }
  },
  computed: {
    formDisabled() {
      if (this.customFormData != null && this.customFormData.id) {
        this.disabled = false
      }
      return this.disabled
    },
  },
  created() {

    if (this.customFormData != null && this.customFormData.id) {
      this.model = this.customFormData
      this.model.packageList=[]
      this.disabled = false
      this.edit(this.customFormData)
    }
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    this.initDictConfig()
    //this.editAfter() //现在子表内容需要

  },
  mounted() {

  },
  methods: {
    initDictConfig() {
      //初始化字典 - 主单位
      initDictOptions('ji_unit').then((res) => {
        var unitDictOptions = this.unitDictOptions
        if (res.success) {
          res.result.forEach(function (item, index) {
            unitDictOptions[item.value] = item.label
          })
        }
        this.unitDictOptions = unitDictOptions
      })
      //初始化字典 - 辅单位
      initDictOptions('zl_unit').then((res) => {
        var zlunitDictOptions = this.zlunitDictOptions
        if (res.success) {
          res.result.forEach(function (item, index) {
            zlunitDictOptions[item.value] = item.label
          })
        }
        this.zlunitDictOptions = zlunitDictOptions
      })
    },
    handlePreviewFile(row) {
      FilePreview(row.patternFile)
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        var datesourse = that.model
        datesourse.crmTradeOrderGoodsList = that.model.erpGoodsPackageList
        datesourse.crmTradeOrderPackageList = []
        if(that.model.erpGoodsPackageList!=null&&that.model.erpGoodsPackageList.length>0) {
          let mergedArray = [];
          this.model.erpGoodsPackageList.forEach(data => {
            mergedArray = mergedArray.concat(data.packageVos.map((item) => {
              return {...item, id: null}
            }))
          })
          datesourse.crmTradeOrderPackageList = mergedArray;
        }
        datesourse.deposit=Number(datesourse.deposit)
        datesourse.createTime = null
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if(!this.model.id){
            httpurl+=this.url.add;
            method = 'post';
          }else{
            httpurl+=this.url.edit;
            method = 'put';
          }
          httpAction(httpurl,datesourse,method).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('ok');
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })

    },
    onSelectChange(selectedRowKeys, selectedRows) {
      if (this.selectedRows.length > 0 && this.selectedRows[0] != null) {
        const key = 'goodsId'
        this.selectedRowKeys = [selectedRowKeys.filter((item) => item !== this.selectedRowKeys[0])[0]]
        this.selectedRows = [selectedRows.filter((item) => item[key] !== this.selectedRows[0][key])[0]]
      } else {
        this.selectedRowKeys = [selectedRowKeys[0]]
        this.selectedRows = [selectedRows[0]]
      }

      this.showPatternFlag = false
      console.log(this.selectedRows, 'this.selectedRows')

      if (this.selectedRows[0] != null) {
        this.selectedProduct = this.selectedRows[0]
        const dataSource = [...this.model.erpGoodsPackageList]
        const target = dataSource.find((item) => item.goodsId === this.selectedRows[0].goodsId)
        console.log('target', target)
        console.log('this.selectedProduct', this.selectedProduct)
        //关联物料包材信息
        if (!this.selectedProduct.packageVos || this.selectedProduct.packageVos.length == 0) {
          this.selectedProduct.packageVos = []

          if (target.packageVos != null && target.packageVos.length > 0) {
            this.model.packageList = target.packageVos
          } else {
            this.model.packageList = []
          }
          this.selectedProduct.packageVos = this.model.packageList
        } else {
          this.model.packageList = this.selectedProduct.packageVos
        }
        if (!this.selectedProduct.packageVos || this.selectedProduct.packageVos.length == 0) {
          this.selectedProduct.packageVos = []

          if (target.packageVos != null && target.packageVos.length > 0) {
            this.model.packageList = target.packageVos
          } else {
            this.model.packageList = []
          }
          this.selectedProduct.packageVos = this.model.packageList
        } else {
          this.model.packageList = this.selectedProduct.packageVos
        }

/*        let formularVos = target.formularVos || this.selectedProduct.formularVos
        this.formularDetailList = []
        if (formularVos && formularVos.length > 0) {
          formularVos.forEach((fitem, index) => {
            let fdetail = {}
            fdetail.rowNo = fitem.id
            fdetail.materialId = fitem.materialId
            fdetail.materialCode = fitem.materialCode
            fdetail.materialName = fitem.materialName
            fdetail.materialSpec = fitem.materialSpec
            fdetail.isSelfProvided = fitem.isSelfProvided
            //fdetail.isSelfProvided_dictText =  fdetail.isSelfProvided=='0'?'否':'是'
            fdetail.formulaProportion = fitem.formulaProportion
            fdetail.taxPrice = fitem.taxPrice
            fdetail.noTaxPrice = fitem.noTaxPrice
            fdetail.price = fitem.price
            fdetail.id = fitem.id
            fdetail.productId = fitem.productId
            fdetail.formulaId = fitem.formulaId
            //fdetail.isClientOwn = fitem.isClientOwn
            //fdetail.actualQuantity = fitem.actualQuantity
            // fdetail.computingFormula = fitem.computingFormula
            // fdetail.formula = fitem.formula

            this.formularDetailList.push(fdetail)
          })

          console.log('获取物料BOM：', this.formularDetailList)
        }*/
      }
    },
    onMainQuantityCellChange(record, dataIndex, value) {
      const dataSource = [...this.model.erpGoodsPackageList]
      const target = dataSource.find((item) => item.packageCode === record.packageCode)
      // console.log(target,key,dataIndex,value)
      if (record) {
        //修改主数量
        target[dataIndex] = value
        //计算辅数量
        target.auxiliaryQuantity = (Number.parseFloat(target.coRate) * Number.parseFloat(value)).toFixed(3)
        this.model.erpGoodsPackageList = dataSource

      }
    },
    onCellChange(record, dataIndex, value) {
      console.log(this.model.erpGoodsPackageList)
      console.log(record)
      const dataSource = [...this.model.erpGoodsPackageList]
      const target = dataSource.find((item) => item.packageCode === record.packageCode)
      // console.log(target,key,dataIndex,value)
      if (record) {
        target[dataIndex] = value
        console.log('isProduction', value, this.erpPackageListTmp)
        if (dataIndex == 'isProduction') {
          //生产
          if (value == '1') {
            const tmpTarget = this.erpPackageListTmp.find((item) => item.packageCode === record.packageCode)
            console.log(tmpTarget, 'tmpTarget')
            if (tmpTarget.formularVos && tmpTarget.formularVos.length > 0) {
              /*              this.formularDetailList = tmpTarget.formularVos
                            target.formularVos = [...tmpTarget.formularVos]
                            this.model.packageList = tmpTarget.packageVos*/
              target.packageVos = [...tmpTarget.packageVos]
              this.model.erpGoodsPackageList = dataSource
              //this.onSelectChange(target.rowKey, this.model.erpGoodsPackageList)
            } else {
              //获取产品物料关联的数据
              // this.getGoodsById(target.goodsId)
            }
          } else {
            //不生产
            /*            this.formularDetailList = []
                        this.model.packageList = []
                        target.formularVos = []
                        target.packageVos = []*/
            this.model.erpGoodsPackageList = dataSource
          }
        } else {
          this.model.erpGoodsPackageList = dataSource
        }
      }
    },
    add(record) {
      getAction(this.url.reviewById, { id: record.desId,reviewId:record.id }).then((res) => {
        let records = res.result
        // if (records != null) {
        /*          Vue.set(record, 'contractCode', records.code)
                  Vue.set(record, 'customerId', records.customerId)
                  Vue.set(record, 'salesman', this.contract.salesman)
                  Vue.set(record, 'customer', records.customer)
                  Vue.set(record, 'weight', records.weight)
                  Vue.set(record, 'weightUnit', records.weightUnit)
                  Vue.set(record, 'payMethod', records.payMethod)
                  Vue.set(record, 'deliveryTime', records.deliveryTime)
                  //Vue.set(record, 'contractTime', records.createTime)
                  Vue.set(record, 'formulaReq', records.formulaReq)
                  Vue.set(record, 'packReq', records.packReq)
                  Vue.set(record, 'erpGoodsPackageList', [])
                  Vue.set(record, 'reviewGoodsFomularList', [])
                  Vue.set(record, 'packageList', [])*/
        this.model = Object.assign({}, record)
        Vue.set(this.model, 'flowFlag', 1)
        Vue.set(this.model, 'reviewId', record.id)
        this.model.id = null
        this.model.status = null
        if(records.bussContractReviewDesRequire!=null) {
          Vue.set(this.model, 'customerId', records.bussContractReviewDesRequire.customerId)
          Vue.set(this.model, 'customer', records.bussContractReviewDesRequire.customer)
          Vue.set(this.model, 'salesman', records.bussContractReviewDesRequire.salesman)
          Vue.set(this.model, 'weight', records.bussContractReviewDesRequire.weight)
          Vue.set(this.model, 'weightUnit', records.bussContractReviewDesRequire.weightUnit)
          Vue.set(this.model, 'cabinetCount', records.bussContractReviewDesRequire.cabinetCount)
          Vue.set(this.model, 'shipPort', records.bussContractReviewDesRequire.shipPort)
          Vue.set(this.model, 'destPort', records.bussContractReviewDesRequire.destPort)
          Vue.set(this.model, 'transactionMethod', records.bussContractReviewDesRequire.transactionMethod)
          Vue.set(this.model, 'payMethod', records.bussContractReviewDesRequire.payMethod)
          Vue.set(this.model, 'daysArrival', records.bussContractReviewDesRequire.daysArrival)
          Vue.set(this.model, 'financeRemark', records.bussContractReviewDesRequire.financeRemark)

          Vue.set(this.model, 'formulaReq', records.bussContractReviewDesRequire.formulaReq)
          Vue.set(this.model, 'packReq', records.bussContractReviewDesRequire.packReq)
        }

        Vue.set(this.model, 'ywActualQuote', records.bussOutletQuoteProductList.reduce((sum, t) => sum + (t.ywActualQuote || 0), 0))

        Vue.set(this.model, 'year', moment().year())
        Vue.set(this.model, 'moneyUnit', '1')


        if(this.model.customerId!=null){
          getAction(this.url.customerById,{id:this.model.customerId}).then((item)=>{
/*            this.model.customerCode=item.code
            this.model.customerName=item.name
            this.model.customerCountryId=item.countryId*/
            let result = item.result
            Vue.set(this.model, 'customerCode', result.code)
            Vue.set(this.model, 'customerName', result.name)
            Vue.set(this.model, 'customerCountryId', result.countryId)
          })
        }

        this.model.bussContractReviewDesRequire = records.bussContractReviewDesRequire
        this.model.bussOutletQuotePackageList = records.bussOutletQuotePackageList
        this.model.bussOutletQuoteProductList = records.bussOutletQuoteProductList
        // }

        //此处可调用出口业务报价单edit，传入出口业务报价单model
        this.visible = true

        this.addAfter()
      })
    },
    addAfter() {
      let _this = this

      _this.model.erpGoodsPackageList = []
      getAction(this.url.productByReviewId, { id: this.model.reviewId }).then((res) => {
        let erpGoodsList = res.result
        erpGoodsList.forEach(function (item, index) {
          console.log(item, index, '获取评审表关联物料')
          let detail = {}
          detail.goodsId = item.id
          detail.rowKey = item.id
          detail.customCode = item.customCode
          detail.packageCode = item.code
          detail.packageName = item.name
          detail.coRate = item.coRate
          detail.noneSpec = item.noneSpec
          detail.meUnit = item.meUnit
          detail.auUnit = item.auUnit
          detail.formulaId = item.formulaId
          detail.formulaCode = item.formulaCode
          detail.formulaNumber = item.formulaNumber
          detail.formulaDefine = item.formulaDefine
          detail.formulaYear = item.formulaYear
          detail.packageType = item.packageType
          detail.packageVos = item.packageVos
          detail.isIncrease = item.isIncrease
          detail.lossRate = item.lossRate
          detail.proportions = item.proportions
          detail.quantity = item.quantity
          detail.formularVos = item.formularVos.map((item) => {
            return { ...item, rowNo: item.id, price:item.noTaxPrice!=null&&item.formulaProportion!=null?Number(item.noTaxPrice*item.formulaProportion*1000).toFixed(2):0}
          })
          detail.number = item.number
          detail.mainQuantity = item.mainQuantity
          detail.auxiliaryQuantity = item.auxiliaryQuantity
          detail.isProduction = item.isProduction
          // _this.filterPackageId(item.id);
          _this.model.erpGoodsPackageList.push(detail)
        })


        if (_this.model.erpGoodsPackageList == null) {
          _this.model.erpGoodsPackageList = []
          _this.isTotalAmountOk = false
          _this.model.totalProductAmount = 0
        } else {
          //_this.selectedRowKeys = _this.model.erpGoodsPackageList.map((item) => item.rowKey)
          //_this.onSelectChange(_this.selectedRowKeys, _this.model.erpGoodsPackageList)
        }
      })
        console.log("_this.model",_this.model)
    },
    edit(record){
      this.model = Object.assign({}, record)
      getAction(this.url.queryCrmTradeOrderGoodsByMainId,{id:record.id}).then((res)=>{
        Vue.set(this.model, 'erpGoodsPackageList', res.result)
      })
    },
  }
}
</script>

<style scoped>

</style>